package com.atguigu.category.service.impl;

import com.atguigu.category.mapper.CategoryMapper;
import com.atguigu.category.service.CategoryService;
import com.atguigu.param.ProductHotParam;
import com.atguigu.pojo.Category;
import com.atguigu.utils.R;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 类别的服务类型
 */

@Service
@Slf4j
public class CategoryServiceImpl implements CategoryService {

    @Autowired
    private CategoryMapper categoryMapper;

    /**
     * 根据类别名称 查询类别对象
     *
     * @param categoryName
     * @return
     */
    @Override
    public R byName(String categoryName) {

        // 封装查询参数
        QueryWrapper<Category> categoryQueryWrapper = new QueryWrapper<>();
        categoryQueryWrapper.eq( "category_name", categoryName );

        // 查询数据库
        Category category = categoryMapper.selectOne(categoryQueryWrapper);

        // 结果封装
        if (category == null) {

            return R.fail("类别查询失败！");
        }
        log.info("CategoryServiceImpl.byName业务结束，结果:{}", "类别查询成功");
        return R.ok("类别查询成功！", category);
    }


    /**
     * 根据传入的热门类别名称集合 返回类别对应的id集合
     *
     * @param productHotParam
     * @return
     */
    @Override
    public R hotsCategory(ProductHotParam productHotParam) {

        // 封装查询参数
        QueryWrapper<Category> queryWrapper = new QueryWrapper<>();

        queryWrapper.in("category_name", productHotParam.getCategoryName());
        queryWrapper.select("category_id");

        // 查询数据库

        List<Object> ids = categoryMapper.selectObjs(queryWrapper);

        R ok = R.ok("多类别热门商品查询成功!", ids);

        log.info("CategoryServiceImpl.hots业务结束，结果:{}", ok);

        return ok;
    }


    /**
     * 查询类别数据
     *
     * @return r 类别数据集合
     */
    @Override
    public R list() {
       List<Category> categoryList = categoryMapper.selectList(null);
        R ok = R.ok("类别数据全部查询成功！", categoryList);
        log.info("CategoryServiceImpl.list业务结束，结果:{}", ok);
       return ok;
    }
}
